//
//  ChargeViewReportController.m
//  Runone
//
//  Created by runone02 on 16/11/10.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "ChargeViewReportController.h"
#import "ParModel.h"
#import "StationRrportModel.h"
#import "RequestHelper.h"
#import "MJExtension.h"
#import "SGImagePickerController.h"
#import "EventTypeTableViewController.h"
#import "UUDatePicker.h"
#import "POPDatePickViewController.h"
#import "CommonUtilityjudge.h"
#import "PromptUtil.h"
#import "DateViewController.h"
#import "AFNetworking.h"
#import "BCNetAPIClient.h"
#import "GPAlbumPickerViewController.h"
#import "StationListPopViewController.h"
#import "UIView+Message.h"
#import "UITextField+ToolBar.h"
#import "UITextView+ToolBar.h"
#import "UIColor+ZXLazy.h"
#import "SVProgressHUD.h"
#import "ROActionSheet.h"
#import "LittleVideoViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
#import "RODateHelper.h"
#import "NSDate+ROCaculateTime.h"
#import "ROImagePickViewController.h"
#import "AJPhotoPickerViewController.h"
#import "AJPhotoBrowserViewController.h"
#import "UIButton+ROCameraPhotoes.h"
#import "DropDownBackView.h"
#import "AppDelegate.h"
#import "BDMTollEventTypeTemplateDefineInfoModel.h"
#import "BDMTollEventTypeDefineInfoModel.h"
#import "DrowDownViewController.h"
#import "UIView+Common.h"
#import "UIButton+ROCameraPhotoes.h"

@interface ChargeViewReportController ()<UIPopoverPresentationControllerDelegate,UITextViewDelegate,UITextFieldDelegate,UIAlertViewDelegate, LittleVideoDelegate,AJPhotoPickerProtocol,AJPhotoBrowserDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
    
    AVPlayer *_player;
    AVPlayerItem *_playItem;
    AVPlayerLayer *_playerLayer;
    AVPlayerLayer *_fullPlayer;
    BOOL _isPlaying;
}

@property (weak, nonatomic) IBOutlet UITextField *StationTF;
@property (weak, nonatomic) IBOutlet UITextField *EventType;
@property (weak, nonatomic) IBOutlet UITextField *monitorTF;
@property (weak, nonatomic) IBOutlet UITextField *EventTimeTF;
@property (weak, nonatomic) IBOutlet UIButton *TimeBtn;
@property (weak, nonatomic) IBOutlet UITextView *DesCription;
@property (weak, nonatomic) IBOutlet UIButton *cancleBtn;
@property (weak, nonatomic) IBOutlet UIButton *commitBtn;
@property (weak, nonatomic) IBOutlet UIScrollView *imageScrollView;
@property (weak, nonatomic) IBOutlet UIButton *stationEventTypeBtn; //收费事件模板按钮

//弹出视图+类+控制器
@property(nonatomic,strong)DateViewController *dateVC;
@property(nonatomic,strong)StationListPopViewController *station;
@property(nonatomic,strong)EventTypeTableViewController *eventVC;
@property(nonatomic,strong) StationRrportModel *StationModel;
@property(nonatomic,strong)POPDatePickViewController *popdate;
@property(nonatomic,strong)PromptUtil *Prompt;
@property(nonatomic,strong)UIAlertView *alert;
@property (weak, nonatomic) IBOutlet UIView *videoV;

//返回结果
@property(nonatomic)NSInteger code;

//Scrollview选图
@property(nonatomic,strong)UIImageView *iv;
@property(nonatomic,strong)UIImage *img;
//@property(nonatomic,strong)NSArray *imageArray;
//@property(nonatomic,strong)UIImage *loadImage;

//Message
@property(nonatomic,strong)UIView *MessageView;
@property(nonatomic,strong)UIAlertView *resultAlert;

@property (nonatomic, strong) ROActionSheet *sheet;

@property(nonatomic,strong)NSMutableArray *loadImageArr;

@property (nonatomic,strong) NSURL *videoUrl;
@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) NSMutableArray *originalImgArr;

@property (strong, nonatomic) NSMutableArray *photos;
@property (assign, nonatomic) NSInteger typeInt;

@property (nonatomic, strong) BDMTollEventTypeDefineInfoModel *currentTemplateModel;
@end

@implementation ChargeViewReportController

- (IBAction)drop:(UIButton *)btn {//事件类型
    __weak typeof(self) weakSelf = self;
    AppDelegate *myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    EventTypeTableViewController *eventVC=[[EventTypeTableViewController alloc] init];
    eventVC.modalPresentationStyle = UIModalPresentationPopover;
    eventVC.LineBlock=^(NSInteger index, NSString *eventTypeStr){
        weakSelf.EventType.text   = eventTypeStr;
        weakSelf.DesCription.text = nil;
        weakSelf.typeInt          = index;
        weakSelf.currentTemplateModel = myDelegate.allTollEvent[index - 1];
        if (weakSelf.currentTemplateModel.TemplateList.count > 1) {
            [weakSelf.stationEventTypeBtn setTitle:[NSString stringWithFormat:@"%@模板",eventTypeStr] forState:UIControlStateNormal];
            BDMTollEventTypeTemplateDefineInfoModel *model = _currentTemplateModel.TemplateList[0];
            weakSelf.DesCription.text = model.TemplateContent;
        }else {
            BDMTollEventTypeTemplateDefineInfoModel *model = weakSelf.currentTemplateModel.TemplateList[0];
            [weakSelf.stationEventTypeBtn setTitle:model.TemplateName forState:UIControlStateNormal];
            [weakSelf stationEventTypeBtnClick:weakSelf.stationEventTypeBtn];
        }
    };
    eventVC.popoverPresentationController.sourceView   = btn;
    eventVC.popoverPresentationController.delegate     = self;
    eventVC.popoverPresentationController.sourceRect   = CGRectMake(130, 138, 0, 0);
    eventVC.popoverPresentationController.permittedArrowDirections = NO;
    [self presentViewController:eventVC animated:YES completion:nil];
}

- (IBAction)stationEventTypeBtnClick:(UIButton *)sender {
//    NSLog(@"%@",_EventType.text);
    if (_EventType.text.length > 0 ) {
        __weak typeof(self) weakSelf = self;
        if (_currentTemplateModel.TemplateList.count > 1) {
            sender.selected = !sender.selected;
            [sender adjustImagePositon:ROCameraPhotoesBtnRight spacing:5];
            
            NSMutableArray *tempNameArray = [[NSMutableArray alloc] init];
            NSMutableArray *contentArray  = [[NSMutableArray alloc] init];
            for (BDMTollEventTypeTemplateDefineInfoModel *tempateModel in _currentTemplateModel.TemplateList) {
                [tempNameArray addObject:tempateModel.TemplateName];
                [contentArray  addObject:tempateModel.TemplateContent];
            }
            DropDownBackView *backVc = [[DropDownBackView alloc] init];
            DrowDownViewController *stationListVC = [[DrowDownViewController alloc] init];
            stationListVC.view.width              = _stationEventTypeBtn.width ;
            stationListVC.dataArray               = tempNameArray;
            stationListVC.view.height             = 44 * (tempNameArray.count);
            backVc.contentController              = stationListVC;
            stationListVC.indexClick              = ^(NSInteger tag, NSString *str) {
                [weakSelf.stationEventTypeBtn setTitle:str forState:UIControlStateNormal];
                [weakSelf.stationEventTypeBtn adjustImagePositon:ROCameraPhotoesBtnRight spacing:5];
                //            [weakSelf.stationEventTypeBtn sizeToFit];
                weakSelf.DesCription.text             = contentArray[tag];
                [backVc dismiss];
            };
            [backVc showFrom:_stationEventTypeBtn];
        }else {
            BDMTollEventTypeTemplateDefineInfoModel *model = _currentTemplateModel.TemplateList[0];
            weakSelf.DesCription.text = model.TemplateContent;
        }
        
    }else {
        [SVProgressHUD showErrorWithStatus:@"请先选择事件类型"];
    }
    
}

- (IBAction)commitBtnClick:(id)sender {
    [self cameraBtnClick];
}

- (void) cameraBtnClick {
    LittleVideoViewController *vc = [[LittleVideoViewController alloc] init];
    vc.delegate = self;
    [self.navigationController presentViewController:vc animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _currentTemplateModel = [[BDMTollEventTypeDefineInfoModel alloc] init];
    
    [_stationEventTypeBtn adjustImagePositon:ROCameraPhotoesBtnRight spacing:5];
    [_stationEventTypeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_stationEventTypeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    
    self.automaticallyAdjustsScrollViewInsets  = NO;
    self.photos = [[NSMutableArray alloc] init];
    [self TFCantEdit];
    [self TFInputOK];
  
    _StationModel                     = [[StationRrportModel alloc] init];
    _station                          = [[StationListPopViewController alloc] init];
    _station.modalPresentationStyle   = UIModalPresentationPopover;
    __weak typeof(self) stationSelf = self;
    _station.LineBlock=^(StationModel *model){
        stationSelf.StationTF.text              =[NSString stringWithFormat:@"%@",model.TollStationName];
        stationSelf.StationModel.TollStationUID = model.TollStationUID;
    };
    
    _Prompt=[[PromptUtil alloc] init];
    _DesCription.delegate=self;

    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    label.text=@"收费事件上报";
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=label;
    
    
    //描述
    _DesCription.layer.borderColor   = [UIColor colorWithRed:236.0/255 green:236.0/255 blue:236.0/255 alpha:1].CGColor;
    _DesCription.layer.cornerRadius  = 2;
    _DesCription.layer.masksToBounds = YES;
    _DesCription.layer.borderWidth   = 1;
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"提 交" style:UIBarButtonItemStylePlain target:self action:@selector(Report)];
}
-(void)Report {
    [self report];
}

-(void)TFInputOK {
    [_monitorTF setTextFieldInputAccessoryView:_monitorTF frame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    _monitorTF.clearButtonMode=UITextFieldViewModeAlways;
    [_DesCription setTextFieldInputAccessoryView:_DesCription frame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
}

-(void)resultMessage:(NSString *)result {
    _resultAlert=[[UIAlertView alloc] initWithTitle:@"提示" message:result delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [_resultAlert show];
}
-(void)TFCantEdit {
    _StationTF.delegate    =self;
    _EventType.delegate    = self;
    _EventTimeTF.delegate  = self;
}
- (IBAction)stationListBtn:(UIButton *)btn {
    _station.popoverPresentationController.sourceView = btn;
    _station.isStation = 1;
    _station.popoverPresentationController.delegate   = self;
    _station.popoverPresentationController.sourceRect = CGRectMake(130, 138, 0,0);
    _station.popoverPresentationController.permittedArrowDirections=NO;
    [self presentViewController:_station animated:YES completion:nil];
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return NO;
}

-(void)textViewDidBeginEditing:(UITextView *)textView {
    if ([_DesCription.text isEqualToString:@"请输入事件描述："]) {
        _DesCription.text=@"";
        _DesCription.textColor=[UIColor blackColor];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length<1) {
        textView.text = @"请输入事件描述：";
        textView.textColor=[UIColor grayColor];
    }
}

-(void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden= YES;
}



- (IBAction)datePick:(UIButton *)btn {
    [self datePick];
    __weak typeof(self) weakSelf=self;
    [self datePick];
    _dateVC.dateBlock=^(NSString *str){
        weakSelf.EventTimeTF.text=str;
    };
    
    [self presentViewController:_dateVC animated:YES completion:nil];
}

-(void)datePick {
    _dateVC=[[DateViewController alloc] init];
    _dateVC.modalPresentationStyle=UIModalPresentationPopover;
    _dateVC.popoverPresentationController.sourceView=self.view;
    _dateVC.popoverPresentationController.sourceRect=DateFrame;
    _dateVC.popoverPresentationController.permittedArrowDirections=NO;
    _dateVC.popoverPresentationController.delegate=self;
}
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}

-(void)request {
    RequestGinsengModel *getCurrentEventModel=[[RequestGinsengModel alloc] init];
    getCurrentEventModel.URL=GetEventDataURL;
    getCurrentEventModel.DataValue = [self dataValueJson];
    getCurrentEventModel.UpdatedTime=[self GetUpdateTime];
    getCurrentEventModel.SystemCode=@"GuangleHighway";
    getCurrentEventModel.MethodName=@"ReportTollStationEventDetailInfo";
    [self requestTestUpdateImage:getCurrentEventModel Image:_originalImgArr];
}
-(void)requestTestUpdateImage:(RequestGinsengModel *)RequestGinseng Image:(NSArray *)LoadImages {

    [SVProgressHUD showWithStatus:@"正在上传,请稍后......"];
    
    NSDictionary *dic=[[NSDictionary alloc] init];
    dic = @{@"methodName":RequestGinseng.MethodName,@"updatedtime":RequestGinseng.UpdatedTime,@"systemcode":DefaultSystemCode,@"DataValue":RequestGinseng.DataValue};
    NSMutableArray *images = [[NSMutableArray alloc] init];
    [images addObjectsFromArray:LoadImages];
    
    [BCNetAPIClient PostVideosAndImages:GetEventDataURL parameters:dic imageArray:images videoArray:_videoUrl.absoluteString success:^(id responseObjecet) {
        if (_videoUrl.absoluteString) {
            [BCNetAPIClient PostVideosAndImages:GetEventDataURL parameters:nil videoArray:_videoUrl.absoluteString success:^(id responseObjecet) {
                [SVProgressHUD showSuccessWithStatus:@"上报成功"];
                [self.navigationController popViewControllerAnimated:YES];
            } failure:^(NSError *error) {
                [SVProgressHUD showSuccessWithStatus:@"上报失败"];
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }else {
            [SVProgressHUD showSuccessWithStatus:@"上报成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
        [SVProgressHUD showSuccessWithStatus:@"上报成功"];
    } failure:^(NSError *error) {
        [SVProgressHUD showSuccessWithStatus:@"上报失败"];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

-(NSString *)GetUpdateTime {
    NSDate *date=[NSDate date];
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *time=[format stringFromDate:date];
    return time;
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex==0) {
        NSLog(@"0");
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(NSString *)dataValueJson {
    
    
//    if ([self.EventType.text isEqualToString:@"收费事件"]) {
//        _StationModel.EventType=@"1";
//    }
//    if ([self.EventType.text isEqualToString:@"冲卡事件"]) {
//        _StationModel.EventType=@"2";
//    }
//    if ([self.EventType.text isEqualToString:@"现场纠纷"]) {
//        _StationModel.EventType=@"3";
//    }
    _StationModel.EventType  = [NSString stringWithFormat:@"%ld",(long)_typeInt];
    _StationModel.DutyMonitor = self.monitorTF.text;
    _StationModel.OccurTime=self.EventTimeTF.text;
    _StationModel.EventDescription=self.DesCription.text;
    _StationModel.TollStationName=self.StationTF.text;
    
    NSMutableDictionary *dic=_StationModel.mj_keyValues;
    NSString *modelStr=[dic mj_JSONString];
    
    ParModel *model=[[ParModel alloc] init];
    model.key=@"TollStationEventInfo";
    model.value=modelStr;
    
    NSMutableDictionary *jsonDic=model.mj_keyValues;
    NSString *json=[jsonDic mj_JSONString];
    
    NSString *str=@"[";
    NSString *temp=[str stringByAppendingString:json];
    NSString *str1=@"]";
    NSString *JsonString=[temp stringByAppendingString:str1];
    
    return JsonString;
}
- (void)report{
    if (stringIsEmpty(_StationTF.text)) {
        _MessageView=MessageViewFrame;
        [_MessageView prompt:@"请选择收费站"];
        [self.view addSubview:_MessageView];
        return;
    }
    if (stringIsEmpty(_EventType.text)) {
        [_Prompt alert:@"请输入事件类型"];
        return;
    }
    if (stringIsEmpty(_monitorTF.text)) {
        [_Prompt alert:@"请输入值班站长"];
        return;
    }
    if (stringIsEmpty(_EventTimeTF.text)) {
        [_Prompt alert:@"请输入发生时间"];
        return;
    }
    if (stringIsEmpty(_DesCription.text)) {
        [_Prompt alert:@"请输入事件描述"];
        return;
    }
    if ([_DesCription.text isEqualToString:@"请输入事件描述："])
    {
        [_Prompt alert:@"请输入事件描述"];
        return;
    }
    if (_DesCription.text.length > 1000) {
        [_Prompt alert:@"事件描述不能超过1000字"];
        return;
    }
    
    NSDate *beginDate = [RODateHelper dateFromString:_EventTimeTF.text withFormat:nil];
    NSInteger beginInter = [beginDate compareToCurrentDate];
    if (beginInter == 1) {
        [_Prompt alert:@"开始时间不能大于当前时间"];
        return;
    }
    
    [self request];
}

- (IBAction)EventTimeFieldClick {
    
}
- (IBAction)uploadImage:(UIButton *)sender {
    
    if (self.originalImgArr.count >= 5) {
        [SVProgressHUD showErrorWithStatus:@"不能选择超过5张图"];
        return;
    }
    
    AJPhotoPickerViewController *picker = [[AJPhotoPickerViewController alloc] init];
    picker.maximumNumberOfSelection = 5 - self.originalImgArr.count;
    picker.multipleSelection        = YES;
    picker.assetsFilter             = [ALAssetsFilter allPhotos];
    picker.showEmptyGroups          = YES;
    picker.delegate                 = self;
    picker.selectionFilter          = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        return YES;
    }];
    
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark - BoPhotoPickerProtocol
- (void)photoPickerDidCancel:(AJPhotoPickerViewController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)photoPicker:(AJPhotoPickerViewController *)picker didSelectAssets:(NSArray *)assets {

    [self.photos addObjectsFromArray:assets];
    _originalImgArr = [[NSMutableArray alloc] init];
    for (int i = 0 ; i < self.photos.count; i++) {
        ALAsset *asset = self.photos[i];
        UIImage *tempImg = [UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
        
        [_originalImgArr addObject:tempImg];
    }
   
    [self configImages];
    [picker dismissViewControllerAnimated:NO completion:nil];
}

- (void)removeDeleteImages {
    for (UIImageView *imageV in self.imageScrollView.subviews) {
        [imageV removeFromSuperview];
    }
}

- (void)configImages {
    self.imageScrollView.hidden = NO;
    CGFloat x = 0;
    CGRect frame = CGRectMake(0, 0, 125, 125);
    for (int i = 0; i < _originalImgArr.count; i++) {
        frame.origin.x = x;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
        [imageView setContentMode:UIViewContentModeScaleAspectFill];
        imageView.clipsToBounds = YES;
        imageView.image = _originalImgArr[i];
        imageView.tag = i;
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showBig:)]];
        [self.imageScrollView addSubview:imageView];
        x += frame.size.width+5;
        self.imageScrollView.contentSize = CGSizeMake(125 * _originalImgArr.count + 5 * _originalImgArr.count - 1, 0);
    }
}

- (void)showBig:(UITapGestureRecognizer *)sender {
    NSInteger index = sender.view.tag;
    AJPhotoBrowserViewController *photoBrowserViewController = [[AJPhotoBrowserViewController alloc] initWithPhotos:self.photos index:index];
    photoBrowserViewController.delegate = self;
    [self presentViewController:photoBrowserViewController animated:YES completion:nil];
}

- (void)photoBrowser:(AJPhotoBrowserViewController *)vc deleteWithIndex:(NSInteger)index {
    [self.originalImgArr removeObjectAtIndex:index];
    [self.photos removeObjectAtIndex:index];
    [self removeDeleteImages];
    [self configImages];
}

- (void)photoBrowser:(AJPhotoBrowserViewController *)vc didDonePhotos:(NSArray *)photos {
    [vc dismissViewControllerAnimated:YES completion:nil];
}

- (void)photoPickerTapCameraAction:(AJPhotoPickerViewController *)picker {
    [SVProgressHUD showErrorWithStatus:@"该功能有待完善"];
//    [self checkCameraAvailability:^(BOOL auth) {
//        if (!auth) {
//            NSLog(@"没有访问相机权限");
//            return;
//        }
//        [picker dismissViewControllerAnimated:NO completion:nil];
//        UIImagePickerController *cameraUI = [UIImagePickerController new];
//        cameraUI.allowsEditing   = NO;
//        cameraUI.delegate        = self;
//        cameraUI.sourceType      = UIImagePickerControllerSourceTypeCamera;
//        cameraUI.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
//        [self presentViewController: cameraUI animated: YES completion:nil];
//    }];
}
- (void)checkCameraAvailability:(void (^)(BOOL auth))block {
    BOOL status = NO;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if(authStatus == AVAuthorizationStatusAuthorized) {
        status = YES;
    } else if (authStatus == AVAuthorizationStatusDenied) {
        status = NO;
    } else if (authStatus == AVAuthorizationStatusRestricted) {
        status = NO;
    } else if (authStatus == AVAuthorizationStatusNotDetermined) {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if(granted){
                if (block) {
                    block(granted);
                }
            } else {
                if (block) {
                    block(granted);
                }
            }
        }];
        return;
    }
    if (block) {
        block(status);
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    UIImage *originalImage;
    if (CFStringCompare((CFStringRef) mediaType,kUTTypeImage, 0)== kCFCompareEqualTo) {
        originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        UIImageWriteToSavedPhotosAlbum(originalImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo {
    if (!error) {
        NSLog(@"保存到相册成功");
        AJPhotoPickerViewController *picker = [[AJPhotoPickerViewController alloc] init];
        picker.maximumNumberOfSelection = 5 - _originalImgArr.count;
        picker.multipleSelection        = YES;
        picker.assetsFilter             = [ALAssetsFilter allPhotos];
        picker.showEmptyGroups          = YES;
        picker.delegate                 = self;
        picker.selectionFilter          = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
            return YES;
        }];
        [self dismissViewControllerAnimated:NO completion:nil];
        [self presentViewController:picker animated:NO completion:nil];
    }else{
        NSLog(@"保存到相册出错%@", error);
    }
}


// *********** - 播放小视频 - *************
#pragma mark -littleVideoDelegate
- (void)finishLittleVideoViewControllerCapture:(NSURL *)filePath {
    _videoUrl = filePath;
    [self crreatPlayViedeo:filePath];
    
}
- (void) crreatPlayViedeo:(NSURL *)videoUrl {
    _playItem = [AVPlayerItem playerItemWithURL:videoUrl];
    _player = [AVPlayer playerWithPlayerItem:_playItem];
    _playerLayer =[AVPlayerLayer playerLayerWithPlayer:_player];
    _playerLayer.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 16, 200);
    _playerLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;//视频填充模式
    [self.videoV.layer addSublayer:_playerLayer];
    [_player play];
//    _isHaveVideo   = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_player pause];
    _player = nil;
    [_sheet hiddenSheet];
}

-(void)uploadImage {
    
}
@end
